package Library.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Libarary.items.IUser;
import Libarary.items.User;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 * @author Sansarova
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		System.out.println(gender);
		String fname = request.getParameter("firstname");
		String lname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String egn = request.getParameter("egn");
		IUser user = new User(username, password, fname, lname,
				Integer.parseInt(age), gender, phone, address, egn);
		if (User.ExistingUser(username, password, fname, lname,
				Integer.parseInt(age), gender, phone, address, egn) == null) {
			boolean isValid =user.canInsertUser();
			System.out.println("Registraciq : "+isValid);
			response.sendRedirect("Index.html");
			System.out
					.println(new User(username, password, fname, lname, Integer
							.parseInt(age), gender, phone, address, egn)
							.print());
		} else {
			response.sendRedirect("Invalid.html");
		}
	}

}
